<?php
/**
 * Astra functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Astra
 * @since 1.0.0
 */
//获得当前分类目录ID
function get_current_category_id() {
$current_category = single_cat_title('', false);
return get_cat_ID($current_category);
}

//获取当前分类的父ID
function get_category_top_parent_id($current_cat_ID) {
  while ($current_cat_ID) {
    $cat = get_category($current_cat_ID);
    $current_cat_ID = $cat->category_parent;
    $catParent = $cat->cat_ID;
  }
  return $catParent;
} 

//获得当前分类目录名称和链接
function _get_category_post($postid){
   $postid = !empty($postid) && $postid > 0 ? $postid : get_the_ID();
   $category = get_the_category($postid);
   // alert($category)
   $id_1 = $category[0]->cat_ID;
   $id_2 = $category[1]->cat_ID;
   if($id_1 > $id_2){
	  $name = $category[0]->name;
	  $link = get_term_link($category[0]->slug, $category[0]->taxonomy); 
   }else{
	   $name = $category[1]->name;
	   $link = get_term_link($category[1]->slug, $category[1]->taxonomy);
   }
   
   return'<a class="active" href="'.$link.'">'.$name.'</a><i></i>';
}

//获取指定分类下指定标签的所有文章
function get_category_tags($args) {
    global $wpdb;
    $tags = $wpdb->get_results
    ("
        SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name
        FROM
            $wpdb->posts as p1
            LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
            LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
            $wpdb->posts as p2
            LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
            LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
        WHERE
            t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
            t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
            AND p1.ID = p2.ID
        ORDER by tag_name
    ");
    $count = 0;
    if($tags) {
        foreach ($tags as $tag) {
            $mytag[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
            $count++;
        }
    } else {
      $mytag = NULL;
    }
    return $mytag;
}


// 获取文章第一张缩略图 
function catch_that_image() {
	global $post;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img*.+src=[\'"]([^\'"]+)[\'"].*>/iU', wp_unslash($post->post_content), $matches);
	if(empty($output)){ 
		$first_img = "https://paperdrips.s3.amazonaws.com/prod/fs/cachedimages/3727365566-08cce0080760678feb08fd1abb3196a2a15c12274eac42fc436be4cc39cceae4.png";
	}else {
		$first_img = $matches [1][0];
	}
	return $first_img;
}


if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Define Constants
 */
define( 'ASTRA_THEME_VERSION', '4.5.1' );
define( 'ASTRA_THEME_SETTINGS', 'astra-settings' );
define( 'ASTRA_THEME_DIR', trailingslashit( get_template_directory() ) );
define( 'ASTRA_THEME_URI', trailingslashit( esc_url( get_template_directory_uri() ) ) );

/**
 * Minimum Version requirement of the Astra Pro addon.
 * This constant will be used to display the notice asking user to update the Astra addon to the version defined below.
 */
define( 'ASTRA_EXT_MIN_VER', '4.5.0' );

